package com.iot.springbootminio.controller;

import com.iot.springbootminio.utils.MinioUtil;
import java.io.File;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/minio")
public class MinioTestController {

    @Resource
    private MinioUtil minioUtil;

    @GetMapping("/test/upload")
    public Boolean putFile() {

        String pathName = "C:\\Users\\lx\\Desktop\\update_point.png";
        String dirName = "20230613";
        String fileName = "update_point.png";

        Boolean aBoolean = minioUtil.putObject(new File(pathName), dirName, fileName);
        log.info("是否上传成功！{}", aBoolean);

        return aBoolean;
    }

    @GetMapping("/test/getFileUrl")
    public String getFileUrl() {

        String dirName = "20230613";
        String fileName = "update_point.png";

        String objectUrl = minioUtil.getObjectUrl(dirName, fileName);
        log.info("文件查询路径 getFileUrl = {}", objectUrl);

        return objectUrl;
    }

    @GetMapping("/test/getFileUrlByParam")
    public String getFileUrlByParam(@RequestParam("dirName") String dirName, @RequestParam("fileName") String fileName) {

        String objectUrl = minioUtil.getObjectUrl(dirName, fileName);
        log.info("文件查询路径 getFileUrlByParam = {}", objectUrl);

        return objectUrl;
    }
}
